package com.pkk.log.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.pkk.components.rpc.request.PagedQueryRequest;
import com.pkk.components.rpc.response.CommonResponse;
import com.pkk.components.rpc.response.util.ResponseUtil;
import com.pkk.log.common.constant.LogSysConstant;
import com.pkk.log.common.reqdto.LogPageReqData;
import com.pkk.log.common.vo.RetryMessageVo;
import com.pkk.log.service.SysLogService;
import com.pkk.support.penguin.business.log.annotation.BusinessLog;
import com.pkk.support.penguin.cat.annotation.MonitorTransaction;
import com.pkk.support.penguin.retry.rabbitmq.annotation.RetryRabbitMqConsumer;
import com.pkk.support.penguin.retry.rabbitmq.constant.RetryConstant;
import com.pkk.support.penguin.retry.rabbitmq.support.RabbitMqProduct;
import com.pkk.support.penguin.retry.rabbitmq.util.RetryUtil;
import io.swagger.annotations.ApiOperation;
import java.io.UnsupportedEncodingException;
import javax.annotation.Resource;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 日志控制器
 * @author: peikunkun
 * @create: 2019-06-10 15:52
 **/
@Slf4j
@RestController
@RequestMapping("sysLog")
public class SysLogController {

  @Resource
  private SysLogService sysLogService;
  @Resource
  private RabbitMqProduct rabbitMqProduct;


  /**
   * @Description: 查询飞鸽用户信息-分页
   * @Param: []
   * @return: com.pkk.components.util.response.CommonResponse
   * @Author: peikunkun
   * @Date: 2019/7/3 0003 上午 11:49
   */
  @BusinessLog(desc = "查询日志的信息-分页")
  @RequestMapping("findLogInfoPage")
  @ApiOperation(value = "查询日志的信息", httpMethod = "POST")
  public CommonResponse findLogInfoPage(@RequestBody @Valid PagedQueryRequest<LogPageReqData> request) {
    return ResponseUtil.success(sysLogService.findLogInfoPage(request));
  }


  /**
   * @Description: 发送队列消息
   * @Param: [r, obg]
   * @return: java.lang.Object
   * @Author: peikunkun
   * @Date: 2019/4/19 0019 下午 3:50
   */
  @MonitorTransaction
  @RequestMapping("sendRetryMsg")
  @BusinessLog(desc = "这是个发送队列测试")
  public Object send(String queue, String obg, Boolean error) {
    String exchange = RetryConstant.QUEUECHANGE_DEFAULT;
    String routeKey = RetryUtil.getRouteKey(queue);
    if (null == error) {
      error = false;
    }

    RetryMessageVo retryMessageVo = new RetryMessageVo();
    retryMessageVo.setError(error);
    retryMessageVo.setObj(obg);
    rabbitMqProduct.send(exchange, routeKey, retryMessageVo);
    return "SUCCESS";
  }


  /**
   * @Description: 重试业务日志队列
   * @Param: [message]
   * @return: void
   * @Author: peikunkun
   * @Date: 2019/6/19 0019 上午 10:45
   */
  @MonitorTransaction
  @RabbitListener(queues = LogSysConstant.QUEUE_RETRY_BUSINESS_LOG)
  @RetryRabbitMqConsumer(queueName = LogSysConstant.QUEUE_RETRY_BUSINESS_LOG, retryCount = 10)
  public void retryBusinessLog(Message message) throws UnsupportedEncodingException {
    final RetryMessageVo retryMessageVo = rabbitMqProduct.convertMessageToJava(message, RetryMessageVo.class);
    log.info("获取的消息为:" + JSONObject.toJSONString(retryMessageVo));
    if (retryMessageVo.isError()) {
      throw new RuntimeException("自定义错误!" + JSONObject.toJSONString(retryMessageVo));
    }
  }

}
